package com.linktel.productionLine.service;

import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.dao.CommitUserDao;
import com.linktel.productionLine.entity.CommitUser;
import com.linktel.productionLine.entity.dto.commitUser.CommitUserDTO;
import com.linktel.productionLine.entity.query.user.AddCommitUserQuery;
import com.linktel.productionLine.entity.query.user.DeleteCommitUserQuery;
import com.linktel.productionLine.entity.query.user.EditCommitUserQuery;
import com.linktel.productionLine.entity.query.user.EditUserPasswordQuery;
import com.linktel.productionLine.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * 对 提交用户 的操作
 *
 * @Author HuangJiaHui
 * @Date 2023/8/30 15:43
 */
@Service
@AllArgsConstructor
public class CommitUserService {

    private final static String INIT_PASSWORD_VALUE = "12345678";
    private CommitUserDao commitUserDao;
    private CommitUserProductionLineService commitUserProductionLineService;

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return 该用户名密码查询到的用户信息
     */
    public CommitUserDTO userLogin(String username, String password) {
        String encryptPassword = DigestUtils.md5DigestAsHex((password).getBytes());
        return commitUserDao.userLogin(username, encryptPassword);
    }

    ;

    /**
     * 根据用户id，展示该用户对应产线的日出勤数据
     *
     * @param commitUserId 用户id
     * @return 日出勤数据列表
     */
    public CommitUserDTO getById(Long commitUserId) {
        return commitUserDao.getById(commitUserId);
    }

    /**
     * 得到所有用户信息
     *
     * @return 用户信息列表
     */
    public List<CommitUserDTO> listCommitUser() {
        return commitUserDao.listCommitUser();
    }

    /**
     * 新增用户
     * 不能重复添加相同账号的用户
     *
     * @param addCommitUserQuery 新增用户信息请求封装
     * @return 是否新增成功
     */
    public Boolean addCommitUser(AddCommitUserQuery addCommitUserQuery) {
        Long currentUserId = addCommitUserQuery.getCurrentUserId();
        String name = addCommitUserQuery.getName();
        String username = addCommitUserQuery.getUsername();
        Integer role = addCommitUserQuery.getRole();
        if (currentUserId == null || currentUserId <= 0) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR, "未登录");
        }
        if (StringUtils.isAnyBlank(name, username, role.toString())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
        }
        List<CommitUser> commitUserList = commitUserDao.getByUsername(username);
        if (!CollectionUtils.isEmpty(commitUserList)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "账号不能跟已有用户的重复");
        }
        String password = DigestUtils.md5DigestAsHex((INIT_PASSWORD_VALUE).getBytes());
        return commitUserDao.addCommitUser(name, username, password, role, currentUserId);

    }

    /**
     * 修改用户
     *
     * @param editCommitUserQuery 更新用户信息请求封装
     * @return 是否修改成功
     */
    @Transactional
    public Boolean editCommitUser(EditCommitUserQuery editCommitUserQuery) {
        Long id = editCommitUserQuery.getId();
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "修改用户id不能为空");
        }
        String name = editCommitUserQuery.getName();
        String username = editCommitUserQuery.getUsername();
        Integer role = editCommitUserQuery.getRole();
        Long currentUserId = editCommitUserQuery.getCurrentUserId();
        if (currentUserId == null || currentUserId <= 0) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR, "未登录");
        }
        commitUserDao.editCommitUser(id, name, username, role, currentUserId);
        if (username != null) {
            List<CommitUser> commitUserList = commitUserDao.getByUsername(username);
            if (commitUserList.size() >= 2) {
                throw new BusinessException(ErrorCode.OPERATION_ERROR, "账号不能跟已有用户的重复");
            }
        }
        return true;

    }

    /**
     * 删除用户
     * 同时删除该用户的所有产线关联信息
     *
     * @param deleteCommitUserQuery 删除用户信息请求封装
     * @return 是否删除成功
     */
    public Boolean deleteCommitUser(DeleteCommitUserQuery deleteCommitUserQuery) {
        Long id = deleteCommitUserQuery.getId();
        Long currentUserId = deleteCommitUserQuery.getCurrentUserId();
        if (id == null || id <= 0 || currentUserId == null || currentUserId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        commitUserProductionLineService.deleteRelationByCommitUserId(id, currentUserId);
        return commitUserDao.deleteCommitUser(id, currentUserId);
    }

    /**
     * 用户修改密码
     *
     * @param editUserPasswordQuery 修改密码请求封装
     * @return 是否修改成功
     */
    public Boolean editUserPassword(EditUserPasswordQuery editUserPasswordQuery) {
        String username = editUserPasswordQuery.getUsername();
        String password = editUserPasswordQuery.getPassword();
        String newPassword = editUserPasswordQuery.getNewPassword();
        String checkPassword = editUserPasswordQuery.getCheckPassword();
        Long currentUserId = editUserPasswordQuery.getCurrentUserId();
        if (currentUserId == null || currentUserId <= 0 || username == null || password == null ||newPassword == null || checkPassword == null || "".equals(username) || "".equals(password) || "".equals(newPassword) || "".equals(checkPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "输入信息不完整");
        }
        String encryptPassword = DigestUtils.md5DigestAsHex((password).getBytes());
        CommitUserDTO commitUserDTO = commitUserDao.userLogin(username, encryptPassword);
        if (commitUserDTO == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户名或密码错误");
        }
        if (newPassword.length() < 4) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "新密码长度至少4位");
        }
        if (!newPassword.equals(checkPassword)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "两次的密码不一致");
        }
        String encryptNewPassword = DigestUtils.md5DigestAsHex((newPassword).getBytes());
        return commitUserDao.editUserPassword(commitUserDTO.getId(), encryptNewPassword, currentUserId);
    }
}
